// cQPServerAndDataBasePage.cpp : implementation file
//

#include "stdafx.h"
#include "QPMig.h"
#include "cQPMigrationSheet.h"
#include "cNotesServerAndDataBasePage.h"
#include "cNotesFolderInfo.h"
#include "helper.h"
#include "noteshelperfunctions.h"
#include "cQuickPlaceDB.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CQPMigApp theApp;

/////////////////////////////////////////////////////////////////////////////
// cQPServerAndDataBasePage property page

IMPLEMENT_DYNCREATE(cQPServerAndDataBasePage, CPropertyPageEx)

cQPServerAndDataBasePage::cQPServerAndDataBasePage() : CPropertyPageEx(cQPServerAndDataBasePage::IDD,0,IDS_QPSERVERANDDATABASESETTINGSHEADERTITLE,IDS_QPSERVERANDDATABASESETTINGSHEADERSUBTITLE)
{
	//{{AFX_DATA_INIT(cQPServerAndDataBasePage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

cQPServerAndDataBasePage::~cQPServerAndDataBasePage()
{
}

void cQPServerAndDataBasePage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(cQPServerAndDataBasePage)
	DDX_Control(pDX, IDC_LIST1, m_lbQuickPlaces);
	DDX_Control(pDX, IDC_COMBO1, m_cbRunningNotesServers);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(cQPServerAndDataBasePage, CPropertyPageEx)
	//{{AFX_MSG_MAP(cQPServerAndDataBasePage)
	ON_BN_CLICKED(IDC_LIST, OnList)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeServer)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// cQPServerAndDataBasePage message handlers

BOOL cQPServerAndDataBasePage::OnSetActive() 
{
	LNText servers;
	int id;
	m_cbRunningNotesServers.ResetContent();
	LNSTATUS status=theApp.stControl.session.GetServerNames(&servers);
	LNINT count=servers.GetCount();
	m_cbRunningNotesServers.AddString("(local)");
	for(LNINT i=0; i<count;i++)
		{
			LNString te=servers[i];
			m_cbRunningNotesServers.AddString(te);
		}
		
	id=m_cbRunningNotesServers.FindString(0,	theApp.stControl.szNotesServerCN);
	if(CB_ERR!=id)
		m_cbRunningNotesServers.SetCurSel(id);
	else
		m_cbRunningNotesServers.SetCurSel(0);
	
	
	
	return CPropertyPageEx::OnSetActive();
}

void cQPServerAndDataBasePage::OnList() 
{
	BOOL bChanged=FALSE;
	if(!theApp.stControl.m_bThrowNotesErrors)
		{
			theApp.stControl.m_bThrowNotesErrors=TRUE;
			LNSetThrowAllErrors( theApp.stControl.m_bThrowNotesErrors);
			bChanged=TRUE;
		}
	LNString server;
	LNString directory=theApp.stControl.m_strQuickPlacePath;
	LNDatabaseArray databases;
	CString cServer;
	m_lbQuickPlaces.ResetContent();
	if (CB_ERR != m_cbRunningNotesServers.GetCurSel())
		{
			m_cbRunningNotesServers.GetLBText(m_cbRunningNotesServers.GetCurSel(),cServer);
			if("(local)" != cServer)
				{
					server=cServer;
					strncpy_s(theApp.stControl.szNotesServerCN,256,cServer,256);
				}
		}
	else
		{
			m_cbRunningNotesServers.GetWindowText(cServer);
			server=cServer;
			strncpy_s(theApp.stControl.szNotesServerCN,256,cServer,256);
		}
	try
		{
			theApp.stControl.pDebugLog->LogOneLine("Trying to enumerate through directory");
			theApp.stControl.pDebugLog->LogOneLine(directory);
			LNSTATUS status=theApp.stControl.session.GetDatabases(&databases,server,directory);
			LNINT count=databases.GetCount();
			CString cQPPath=theApp.stControl.m_strQuickPlacePath;
			cQPPath.MakeLower();
			for(LNINT i=0; i<count;i++)
				{
					LNDatabase base=databases[i];
					CString cTemp=base.GetFilepath();
					cTemp.MakeLower();
					if(cTemp.Find("\\main.nsf")>0)
						{
							if((cTemp.Find("\\help\\")>0) || (cTemp.Find(cQPPath)>0) || (cTemp.Find("\\tutorial\\")>0))
								;
							else
								{
									cTemp=cTemp.Mid(cQPPath.GetLength()+1);
									cTemp=cTemp.Left(cTemp.Find("\\"));
									if("areatypes"!=cTemp)
										m_lbQuickPlaces.AddString(cTemp);
								}
						}
				}
			GetDlgItem(IDC_LIST1)->SetFocus();
		}
	catch(LNSTATUS error)
		{
			char errorBuf[LNERROR_MESSAGE_LENGTH];
			LNGetErrorMessage(error, errorBuf);
			AfxMessageBox(errorBuf,MB_OK|MB_ICONERROR);
			theApp.stControl.pDebugLog->LogOneLine(errorBuf);
		}
	
	if(bChanged)
		{
			theApp.stControl.m_bThrowNotesErrors=FALSE;
			LNSetThrowAllErrors( theApp.stControl.m_bThrowNotesErrors);
		}

	
}

LRESULT cQPServerAndDataBasePage::OnWizardNext() 
{
	theApp.stControl.pDebugLog->LogOneLine("Entering cQPServerAndDataBasePage::OnWizardNext()");
	theApp.stControl.pDebugLog->Flush();
	LNSetThrowAllErrors( TRUE);

	int count=m_lbQuickPlaces.GetSelCount();
	
	
	if(count >= 1)
		{
			if(count>1)
				{
					theApp.stControl.m_bAdvanced=FALSE;
					theApp.stControl.m_bSubRoomSecurity=TRUE;
				}
			theApp.stControl.m_bSubRoomSecurity=TRUE;
			if(!theApp.stControl.pLog)
				theApp.stControl.pLog= new cLogger(CString(theApp.stControl.m_strDirectoryPath+"result.txt"));
			for(int i=0;i<m_lbQuickPlaces.GetCount();i++)
				{
					if(!m_lbQuickPlaces.GetSel(i))
						continue;
					m_lbQuickPlaces.GetText(i,theApp.stControl.m_strQPName);
					cQuickPlaceDB *cQPdb =NULL;
					CString cNotesDB=CString(theApp.stControl.m_strQuickPlacePath+"\\"+theApp.stControl.m_strQPName+"\\Main.nsf");
					cQPdb = new cQuickPlaceDB( theApp.stControl.szNotesServerCN,(LPCSTR)cNotesDB,theApp.stControl.m_strQPName);
					
					theApp.stControl.pDebugLog->LogOneLine(cNotesDB);
					theApp.stControl.pDebugLog->Flush();
					//strncpy(theApp.stControl.szNotesDatabaseCN,CString(theApp.stControl.m_strQuickPlacePath+"\\"+theApp.stControl.m_strQPName+"\\main.nsf"),255);

					CString cDirectoryName=cQPdb->szNotesDatabaseCN;//theApp.stControl.szNotesDatabaseCN;
					int Pos=cDirectoryName.ReverseFind('\\');
					if(Pos>0)
						cDirectoryName=cDirectoryName.Left(Pos);
					Pos=cDirectoryName.ReverseFind('\\');
					if(Pos>0)
						cDirectoryName=cDirectoryName.Mid(Pos+1);
					DWORD dwErrCode=0;	
					theApp.stControl.m_strCurDir=theApp.stControl.m_strDirectoryPath+theApp.stControl.m_strQPName;
					CFileStatus status;
					CString cF=CString(theApp.stControl.m_strDirectoryPath+"struct.xml");
					DWORD dwFlag=GetFileAttributes(cF);

					if(INVALID_FILE_ATTRIBUTES!=dwFlag) 
						{	
							DWORD dwFlag;
							CString cInfo;
							cInfo.Format("An existing export (%s) is detected, remove it before continuing?",theApp.stControl.m_strCurDir);

							if(IDYES==AfxMessageBox(cInfo,MB_YESNO|MB_ICONINFORMATION))
								{
									cInfo.Format("This will delete everything in the directory (%s) and its subdirectories are you sure that you want to do that?",theApp.stControl.m_strCurDir);

									if(IDYES==AfxMessageBox(cInfo,MB_YESNO|MB_ICONINFORMATION))
										if(DeleteDirectory(theApp.stControl.m_strCurDir,&dwFlag))
											{
												SetCurrentDirectory("..");
												RemoveDirectory(theApp.stControl.m_strCurDir);
											}
									}
								
							
						}

					if(!CreateQPDirectory(theApp.stControl.m_strCurDir,&dwErrCode))
						{
							LPVOID lpMsgBuf;
							FormatMessage( 
									FORMAT_MESSAGE_ALLOCATE_BUFFER | 
									FORMAT_MESSAGE_FROM_SYSTEM | 
									FORMAT_MESSAGE_IGNORE_INSERTS,
									NULL,
									dwErrCode,
									MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
									(LPTSTR) &lpMsgBuf,
									0,
									NULL 
							);
							
							CString cInfo;
							cInfo.Format("Could not create the directory %s\r\nThe error message is %s",theApp.stControl.m_strCurDir,lpMsgBuf );
							AfxMessageBox(cInfo,MB_OK|MB_ICONERROR);
							theApp.stControl.pLog->LogOneLine(cInfo);
							// Free the buffer.
							LocalFree(lpMsgBuf);
							return -1;

						}
					SetCurrentDirectory(theApp.stControl.m_strCurDir);
					
					if(!theApp.stControl.pdb) //Only do this once
						{
							CreateListTemplates();
						}

					try
						{
							theApp.stControl.pdb=cQPdb;

							if(cQPdb->Process())
								{
									theApp.stControl.m_objQuickPlaces.Add(cQPdb);
								}
						}
					catch(LNSTATUS error)
						{
							char errorBuf[LNERROR_MESSAGE_LENGTH];
							LNGetErrorMessage(error, errorBuf);
							AfxMessageBox(errorBuf,MB_OK|MB_ICONERROR);
							theApp.stControl.pDebugLog->LogOneLine(errorBuf);
							return -1;
						}

				}
			}
	else
		{
			AfxMessageBox("Please choose the database",MB_OK|MB_ICONINFORMATION);
			return -1;
		}
	LNSetThrowAllErrors( FALSE);
	theApp.stControl.pDebugLog->LogOneLine("Exiting cQPServerAndDataBasePage::OnWizardNext()");
	theApp.stControl.pDebugLog->Flush();
	
	if(theApp.stControl.m_bAdvanced)
		return CPropertyPageEx::OnWizardNext();
	else
		{
			((CPropertySheet*)GetParent())->SetActivePage( 8);
			return -1;
		}


}

void cQPServerAndDataBasePage::OnSelchangeServer() 
{
	OnList();
	
}

void cQPServerAndDataBasePage::OnDblclkList1() 
{
	int nPage;
	CPropertySheet* psheet = (CPropertySheet*) GetParent();   
	if(psheet)
		{
			nPage=psheet->GetActiveIndex();
			if(OnWizardNext()>=0)
				psheet->SetActivePage(nPage+1);
		}
	
}

void cQPServerAndDataBasePage::CreateListTemplates()
{
	
	theApp.stControl.pDebugLog->LogOneLine("Entering cQPServerAndDataBasePage::CreateListTemplates()");
	theApp.stControl.pDebugLog->Flush();
	MSXML2::IXMLDOMDocument2Ptr pListTemplateDoc=NULL;
	MSXML2::IXMLDOMElementPtr pNode=NULL;
	try
		{
			HRESULT hr=pListTemplateDoc.CreateInstance(__uuidof(DOMDocument40));
			if(FAILED(hr))
				{
					AfxMessageBox("Could not instantiate the DOMDocument component\r\nPlease make sure that the Microsoft XML Parser is installed",MB_OK|MB_ICONERROR);
					theApp.stControl.pLog->LogOneLine("Could not instantiate the DOMDocument component\r\nPlease make sure that the Microsoft XML Parser is installed");
						
					return;
				}
			CWaitCursor cWait;
			MSXML2::IXMLDOMProcessingInstructionPtr pPI = pListTemplateDoc->createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

			_variant_t vNullVal;
			vNullVal.vt = VT_NULL;
			pListTemplateDoc->insertBefore(pPI, vNullVal);
			pPI.Release();

			MSXML2::IXMLDOMElementPtr pRootElem=pListTemplateDoc->createElement(L"listtemplates");
			if(pRootElem)
				{
					pListTemplateDoc->documentElement=pRootElem;
					MSXML2::IXMLDOMElementPtr pContainerElem=pListTemplateDoc->createElement(L"listtemplate");
					if(pContainerElem)
						{
							theApp.stControl.pLog->LogOneLine("Creating listtemplate QPEvents");
							hr=pContainerElem->setAttribute(L"name", L"QPEvents");

							pNode=pListTemplateDoc->createElement(L"name");
							pNode->put_text(L"QPEvents");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Title");
							pNode->put_text(L"QPEvents");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"BaseType");
							pNode->put_text(L"Events");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Description");
							pNode->put_text(L"This information is migrated from QuickPlace");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableattachments");
							pNode->put_text(L"true");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableversioning");
							pNode->put_text(L"false");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							MSXML2::IXMLDOMElementPtr pFieldsRootElem=pListTemplateDoc->createElement(L"fields");
							if(pFieldsRootElem)
								{
					
									MSXML2::IXMLDOMElementPtr pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"Location");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Location");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"QP.Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Original Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									//pFieldsElem=pListTemplateDoc->createElement(L"field");
									//if(pFieldsElem)
									//	{
									//		pNode=pListTemplateDoc->createElement(L"name");
									//		pNode->put_text(L"Created By");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"displayname");
									//		pNode->put_text(L"Created By");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"extended-type");
									//		pNode->put_text(L"User");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"required");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"addtodefaultview");
									//		pNode->put_text(L"true");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"richtext");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();
									//		
									//		pNode=pListTemplateDoc->createElement(L"groupby");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();
									//	}


									//pFieldsRootElem->appendChild(pFieldsElem);
									//pFieldsElem.Release();
								}
							pContainerElem->appendChild(pFieldsRootElem);
							pFieldsRootElem.Release();
						}
					pRootElem->appendChild(pContainerElem);
					pContainerElem.Release();
					pContainerElem=pListTemplateDoc->createElement(L"listtemplate");
					if(pContainerElem)
						{
							theApp.stControl.pLog->LogOneLine("Creating listtemplate QPDocLib");

							hr=pContainerElem->setAttribute(L"name", L"QPDocLib");

							pNode=pListTemplateDoc->createElement(L"name");
							pNode->put_text(L"QPDocLib");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Title");
							pNode->put_text(L"QPDocLib");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"BaseType");
							pNode->put_text(L"DocumentLibrary");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Description");
							pNode->put_text(L"This is the folder containing the migrated documents");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableattachments");
							pNode->put_text(L"false");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableversioning");
							pNode->put_text(L"false");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							MSXML2::IXMLDOMElementPtr pFieldsRootElem=pListTemplateDoc->createElement(L"fields");
							if(pFieldsRootElem)
								{
					
									MSXML2::IXMLDOMElementPtr pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"QP.Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Original Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}

									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"QuickPlace document title");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"QuickPlace document title");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									//pFieldsElem=pListTemplateDoc->createElement(L"field");
									//if(pFieldsElem)
									//	{
									//		pNode=pListTemplateDoc->createElement(L"name");
									//		pNode->put_text(L"Description");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"displayname");
									//		pNode->put_text(L"Description");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"extended-type");
									//		pNode->put_text(L"Note");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"required");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"addtodefaultview");
									//		pNode->put_text(L"true");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"richtext");
									//		pNode->put_text(L"true");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();
									//		
									//		pNode=pListTemplateDoc->createElement(L"groupby");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();
									//	}


									//pFieldsRootElem->appendChild(pFieldsElem);
									//pFieldsElem.Release();
									for(int iCustomField=0;iCustomField<=theApp.stControl.m_objArrayCustomFields.GetUpperBound();iCustomField++)
										{
											cNotesFieldInfo *pCustomField=(cNotesFieldInfo *)theApp.stControl.m_objArrayCustomFields.GetAt(iCustomField);
											if(pCustomField)
												{
													MSXML2::IXMLDOMElementPtr pFieldsElem=pListTemplateDoc->createElement(L"field");
													if(pFieldsElem)
														{
															pNode=pListTemplateDoc->createElement(L"name");
															BSTR bstrField=pCustomField->m_strSharePointDisplayName.AllocSysString();
															pNode->put_text(bstrField);
															pFieldsElem->appendChild(pNode);
															pNode.Release();

															pNode=pListTemplateDoc->createElement(L"displayname");
															pNode->put_text(bstrField);
															pFieldsElem->appendChild(pNode);
															pNode.Release();
															::SysFreeString(bstrField);

															pNode=pListTemplateDoc->createElement(L"extended-type");
															bstrField=pCustomField->m_strSharePointFieldType.AllocSysString();
															pNode->put_text(bstrField);
															pFieldsElem->appendChild(pNode);
															pNode.Release();
															::SysFreeString(bstrField);

															pNode=pListTemplateDoc->createElement(L"required");
															pNode->put_text(L"false");
															pFieldsElem->appendChild(pNode);
															pNode.Release();

															pNode=pListTemplateDoc->createElement(L"addtodefaultview");
															pNode->put_text(L"true");
															pFieldsElem->appendChild(pNode);
															pNode.Release();

															pNode=pListTemplateDoc->createElement(L"richtext");
															pNode->put_text(L"false");
															pFieldsElem->appendChild(pNode);
															pNode.Release();
															
															pNode=pListTemplateDoc->createElement(L"groupby");
															pNode->put_text(L"false");
															pFieldsElem->appendChild(pNode);
															pNode.Release();
														}

													pFieldsRootElem->appendChild(pFieldsElem);
													pFieldsElem.Release();
												}

										}


/*									pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"Related Documents");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Related Documents");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Note");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
										}

									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									*/
								}
							pContainerElem->appendChild(pFieldsRootElem);
							pFieldsRootElem.Release();
							/*
							IXMLDOMElementPtr pViewsRootElem=pListTemplateDoc->createElement(L"views");
							if(pViewsRootElem)
								{
									IXMLDOMElementPtr pVievElem=pListTemplateDoc->createElement(L"view");
									if(pVievElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"By Topic");
											pVievElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"defaultview");
											pNode->put_text(L"false");
											pVievElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"query");
											pNode->put_text(L"<GroupBy Collapse=\"FALSE\"><FieldRef Name=\"QP_x0020_Title\" /></GroupBy>");
											pVievElem->appendChild(pNode);
											pNode.Release();

											pFieldsRootElem=pListTemplateDoc->createElement(L"viewfields");
											if(pFieldsRootElem)
												{
													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"DocIcon");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"LinkFilename");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"Author");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"Created");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"Modified By");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"Modified");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"LinkCheckedOutTitle");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"Title");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pNode=pListTemplateDoc->createElement(L"name");
													pNode->put_text(L"Original Author");
													pFieldsRootElem->appendChild(pNode);
													pNode.Release();

													pVievElem->appendChild(pFieldsRootElem);
													pFieldsRootElem.Release();
												}

											pViewsRootElem->appendChild(pVievElem);
											pVievElem.Release();
										}
									pContainerElem->appendChild(pViewsRootElem);
									pViewsRootElem.Release();
								}
							*/

						}
					pRootElem->appendChild(pContainerElem);
					pContainerElem.Release();
					pContainerElem=pListTemplateDoc->createElement(L"listtemplate");
					if(pContainerElem)
						{
							theApp.stControl.pLog->LogOneLine("Creating listtemplate QPDiscussions");

							hr=pContainerElem->setAttribute(L"name", L"QPDiscussions");

							pNode=pListTemplateDoc->createElement(L"name");
							pNode->put_text(L"QPDiscussions");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Title");
							pNode->put_text(L"QPDiscussions");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"BaseType");
							pNode->put_text(L"DiscussionBoard");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Description");
							pNode->put_text(L"This is the folder containing the migrated discussions");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableattachments");
							pNode->put_text(L"true");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableversioning");
							pNode->put_text(L"false");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							MSXML2::IXMLDOMElementPtr pFieldsRootElem=pListTemplateDoc->createElement(L"fields");
							if(pFieldsRootElem)
								{
					
									MSXML2::IXMLDOMElementPtr pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"QP.Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Original Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
								}
							pContainerElem->appendChild(pFieldsRootElem);
							pFieldsRootElem.Release();
						}
					pRootElem->appendChild(pContainerElem);
					pContainerElem.Release();			

					pContainerElem=pListTemplateDoc->createElement(L"listtemplate");
					if(pContainerElem)
						{
							theApp.stControl.pLog->LogOneLine("Creating listtemplate QPTasks");

							hr=pContainerElem->setAttribute(L"name", L"QPTasks");

							pNode=pListTemplateDoc->createElement(L"name");
							pNode->put_text(L"QPTasks");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Title");
							pNode->put_text(L"QPTasks");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"BaseType");
							pNode->put_text(L"Tasks");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Description");
							pNode->put_text(L"This is the folder containing the migrated tasks");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableattachments");
							pNode->put_text(L"true");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableversioning");
							pNode->put_text(L"false");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							MSXML2::IXMLDOMElementPtr pFieldsRootElem=pListTemplateDoc->createElement(L"fields");
							if(pFieldsRootElem)
								{
					
									MSXML2::IXMLDOMElementPtr pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"QP.Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Original Author");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"QP.AssignedTo");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"QP Assigned To");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Text");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
									//pFieldsElem=pListTemplateDoc->createElement(L"field");
									//if(pFieldsElem)
									//	{
									//		pNode=pListTemplateDoc->createElement(L"name");
									//		pNode->put_text(L"Created By");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"displayname");
									//		pNode->put_text(L"Created By");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"extended-type");
									//		pNode->put_text(L"Text");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"required");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"addtodefaultview");
									//		pNode->put_text(L"true");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();

									//		pNode=pListTemplateDoc->createElement(L"richtext");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();
									//		
									//		pNode=pListTemplateDoc->createElement(L"groupby");
									//		pNode->put_text(L"false");
									//		pFieldsElem->appendChild(pNode);
									//		pNode.Release();
									//	}

									//

									//pFieldsRootElem->appendChild(pFieldsElem);
									//pFieldsElem.Release();

								}
							pContainerElem->appendChild(pFieldsRootElem);
							pFieldsRootElem.Release();
						}
					pRootElem->appendChild(pContainerElem);
					pContainerElem.Release();

					pContainerElem=pListTemplateDoc->createElement(L"listtemplate");
					if(pContainerElem)
						{
							theApp.stControl.pLog->LogOneLine("Creating listtemplate QPAnnouncements");
							hr=pContainerElem->setAttribute(L"name", L"QPAnnouncements");

							pNode=pListTemplateDoc->createElement(L"name");
							pNode->put_text(L"QPAnnouncements");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Title");
							pNode->put_text(L"QPAnnouncements");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"BaseType");
							pNode->put_text(L"Announcements");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"Description");
							pNode->put_text(L"This information is migrated from QuickPlace");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableattachments");
							pNode->put_text(L"true");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							pNode=pListTemplateDoc->createElement(L"enableversioning");
							pNode->put_text(L"false");
							pContainerElem->appendChild(pNode);
							pNode.Release();

							MSXML2::IXMLDOMElementPtr pFieldsRootElem=pListTemplateDoc->createElement(L"fields");
							if(pFieldsRootElem)
								{
					
									MSXML2::IXMLDOMElementPtr pFieldsElem=pListTemplateDoc->createElement(L"field");
									if(pFieldsElem)
										{
											pNode=pListTemplateDoc->createElement(L"name");
											pNode->put_text(L"Body");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"displayname");
											pNode->put_text(L"Body");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"extended-type");
											pNode->put_text(L"Note");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"required");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"addtodefaultview");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();

											pNode=pListTemplateDoc->createElement(L"richtext");
											pNode->put_text(L"true");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
											
											pNode=pListTemplateDoc->createElement(L"groupby");
											pNode->put_text(L"false");
											pFieldsElem->appendChild(pNode);
											pNode.Release();
										}
									pFieldsRootElem->appendChild(pFieldsElem);
									pFieldsElem.Release();
								}
							pContainerElem->appendChild(pFieldsRootElem);
							pFieldsRootElem.Release();
						}
					pRootElem->appendChild(pContainerElem);
					pContainerElem.Release();							


				}
			CString cf;
			cf.Format("%s%s",theApp.stControl.m_strDirectoryPath,"listtemplates.xml");
			BSTR bstr=cf.AllocSysString();
			pListTemplateDoc->save(bstr);
			::SysFreeString(bstr);
			theApp.stControl.pDebugLog->LogOneLine("Exiting cQPServerAndDataBasePage::CreateListTemplates()");
			theApp.stControl.pDebugLog->Flush();
			
		}
		catch (CException* cEx) 
		{
	    TCHAR   szCause[255];

      cEx->GetErrorMessage(szCause, 255);
      MessageBox(szCause, MB_OK);
			return;
		}

}
